package com.kaiupup.glsc.study.product;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;

import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class NacosConfigDemo {
    public static void main(String[] args) {
        String serverAddr = "localhost:8848";
        String namespace = "your_namespace_id";  // 如有命名空间
        Properties properties = new Properties();
        properties.put("serverAddr", serverAddr);
        properties.put("namespace", namespace);
        // 若开启鉴权
        // properties.put("username", "nacos");
        // properties.put("password", "nacos");

        try {
            ConfigService configService = NacosFactory.createConfigService(properties);
            System.out.println("连接Nacos成功！");
        } catch (NacosException e) {
            e.printStackTrace();
            Throwable cause = e.getCause();
            if (cause instanceof InvocationTargetException) {
                Throwable targetEx = ((InvocationTargetException) cause).getTargetException();
                targetEx.printStackTrace();
            }
        }
    }
}